/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avenue;

import javax.swing.JOptionPane;
import model.AppUser;
import model.DBManager;
import model.RolePermission;
import avenue.Utils;

/**
 *
 * @author Aggelos
 */
public class Session {

    public static AppUser loginUser;
    public static final int USERS_MODULE = 1;
    public static final int CUSTOMERS_MODULE = 2;
    public static final int PROGRAMS_MODULE = 3;
    public static final int TOLL_COLLECTION_MODULE = 4;
    public static final int PAYMENTS_MODULE = 5;
    public static final int LOG_MODULE = 6;
    public static final int CAN_CREATE = 1;
    public static final int CAN_RETRIEVE = 2;
    public static final int CAN_UPDATE = 3;
    public static final int CAN_DELETE = 4;
    public static final int STATION_LEVEL_ACCESS = 5;

    public Session() {
    }
/*
    public static boolean doesSessionUserHasPermission(int permission) {
        boolean ret = false;
        if (loginUser.getId() == null) {
            return false;
        }
        if (loginUser.getRoleId() == null) {
            return false;
        }
        if (loginUser.getRoleId().getSystemProtected() == 1) {
            return true;
        }
        if (loginUser.getRoleId().getRolePermissionList() == null) {
            return false;
        }
        for (RolePermission rp : loginUser.getRoleId().getRolePermissionList()) {
            if (rp.getModuleId().getId() == permission) {
                return (rp.getCanRetrieve() == 1);
            }
        }
        return ret;
    }
*/

    public static boolean userHasRolePermission(int m, int rolePermission) {
        RolePermission rp;
        try {
            //Ο system administrator που είναι και protected έχει πρόσβαση σε όλα!!!
            if (Session.loginUser.getSystemProtected()==1){
                return true;
            }
            rp = (RolePermission) DBManager.em.createQuery("select rp from RolePermission rp where rp.moduleId.id=:moduleId and rp.roleId=:roleId").setParameter("moduleId", m).setParameter("roleId", loginUser.getRoleId()).getSingleResult();
            boolean r = false;

            if (rolePermission == CAN_CREATE) {
                r = (rp.getCanCreate() == 1);
                if (!r) {
                    Utils.showMessage(null, String.format(Utils.msgYouHaveNoPermission,"δημιουργίας"), Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
                }

            } else if (rolePermission == CAN_RETRIEVE) {
                r = (rp.getCanRetrieve() == 1);
                /*if (!r) {
                    Utils.showMessage(null, String.format(Utils.msgYouHaveNoPermission,"αναζήτησης"), Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
               }*/
            } else if (rolePermission == CAN_UPDATE) {
                r = (rp.getCanUpdate() == 1);
                if (!r) {
                    Utils.showMessage(null, String.format(Utils.msgYouHaveNoPermission,"επεξεργασίας"), Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
               }
            } else if (rolePermission == CAN_DELETE) {
                r = (rp.getCanDelete() == 1);
                if (!r) {
                   Utils.showMessage(null, String.format(Utils.msgYouHaveNoPermission,"διαγραφής"), Utils.msgDialogTitle, JOptionPane.WARNING_MESSAGE);
                }
            } else if (rolePermission == STATION_LEVEL_ACCESS) {
                r = (rp.getStationLevelAccess() == 1);
            }


            return r;

        } catch (Exception ex) {
           // Utils.mainForm.log("ERROR: "+ex.getMessage());
            return false;
        }
    }
}
